<template>
  <ex-dialog
    width="1000px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <template>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm" :disabled="disabled">
          <el-row>
              <el-col :span="18">
                <el-form-item label="签约日期：" prop="signTime">
                  <el-date-picker
                    v-model="ruleForm.signTime"
                    align="right"
                    format="yyyy-MM-dd" 
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="服务时间：" prop="date">
                  <el-date-picker clearable v-model="ruleForm.date" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"
                    placeholder="请选择">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="服务类型：" prop="serviceTypeList">
                  <el-select v-model="ruleForm.serviceTypeList" filterable multiple placeholder="请选择" size="small" >
                    <el-option v-for="item in serviceList" :key="item.id" :label="item.value" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="服务佣金：" prop="serviceCommission">
                  <el-input v-model="ruleForm.serviceCommission" placeholder="请输入服务佣金">
                    <template slot="append">%</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="总直播场数直播场数：">
                  <el-input v-model="ruleForm.liveNum" placeholder="不可填写，根据下方填写数值累加得出" disabled>
                    <template slot="append">场</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="专场直播场数：" prop="specialLiveNum">
                  <el-input v-model="ruleForm.specialLiveNum" placeholder="请输入专场直播场数" @input="calculateLiveNum">
                    <template slot="append">场</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="串场直播场数：" prop="guestLiveNum">
                  <el-input v-model="ruleForm.guestLiveNum" placeholder="请输入串场直播场数" @input="calculateLiveNum">
                    <template slot="append">场</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="短视频总条数：">
                  <el-input v-model="ruleForm.shortVideoNum" placeholder="不可填写，根据下方填写数值累加得出" disabled>
                    <template slot="append">条</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="达人探店条数：" prop="talentShortVideoNum">
                  <el-input v-model="ruleForm.talentShortVideoNum" placeholder="请输入达人探店条数" @input="calculateshortVideoNum()">
                    <template slot="append">条</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="云剪辑条数：" prop="cloudShortVideoNum">
                  <el-input v-model="ruleForm.cloudShortVideoNum" placeholder="请输入云剪辑条数" @input="calculateshortVideoNum()">
                    <template slot="append">条</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="视频宣传拍摄条数：" prop="publicizeShortVideoNum">
                  <el-input v-model="ruleForm.publicizeShortVideoNum" placeholder="请输入视频宣传拍摄条数" @input="calculateshortVideoNum()">
                    <template slot="append">条</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="前置费：" prop="frontMoney">
                  <el-input v-model="ruleForm.frontMoney" placeholder="请输入服务佣金">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="前置费收取日期：" prop="frontMoneyTime">
                  <el-date-picker
                    v-model="ruleForm.frontMoneyTime"
                    type="date"
                    format="yyyy-MM-dd" 
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
        </el-form>
      </template>
    </div>
    <div class="button_box">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
        >保存提交</el-button
      >
    </div>
  </ex-dialog>
</template>
  <script>
import Loading from "@/components/Loading/index";
// api
import {
  addClient,
  editService,
  serviceInfo,
} from "@/api/customer/customerFile";
import { shopTypeTree } from "@/api/merchants";
import { getCity } from "@/api/clientManagement";
export default {
  components: {
    Loading,
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    var checkServiceCommission = (rule, value, callback) => {
      //销售原价校验
      let regNumber = /(^[0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/; //大于0的数值且最多保留两位小数的数值
      if (value === "") {
        return callback(new Error("请输入服务佣金"));
      } else {
        if (value < 0 || value > 100) {
          return callback(new Error("请填写0-100的数值"));
        } else {
          console.log("value", regNumber.test(value));
          if (
            regNumber.test(value) == false ||
            value === "0.00" ||
            value === "0.0"
          ) {
            return callback(new Error("请填写大于0且最多保留两位小数的数值"));
          } else {
            return callback();
          }
          dd;
        }
      }
    };
    var checkfrontMoney = (rule, value, callback) => {
      //销售原价校验
      let regNumber = /(^[0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/; //大于0的数值且最多保留两位小数的数值
      if(value !== ''){
        if ( regNumber.test(value) == false || value === "0.00" || value === "0.0" ) {
          return callback(new Error("请填写大于0且最多保留两位小数的数值"));
        }else {
          return callback();
        } 
      }else {
        return callback();
      } 
      
    };
    var checkConsumeCycleRatio = (rule, value, callback) => {
      let regNumber = /^\+?[0-9]\d*$/; //大于0的正整数
      if (value === "") {
        return callback(new Error("请输入数值"));
      } else if (regNumber.test(value) == false) {
        return callback(new Error("请输入大于等于0的整数"));
      } else {
        return callback();
      }
    };
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "编辑服务信息",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        type: '',//操作类型 1 修改签约状态 2 编辑服务信息
        signTime: '',//签约时间
        serviceTimeEnd: '',//服务时间结束
        serviceTimeStart: '',//服务时间开始
        serviceTypeList: [],//服务类型 bee_client_service_type
        serviceCommission: null,//服务佣金（单位：百分比）
        liveNum: null,//直播场次
        specialLiveNum: null,//专场直播场数
        guestLiveNum: null,//串场直播场数

        shortVideoNum: null,//短视频总条数
        talentShortVideoNum: null,//达人探店条数
        cloudShortVideoNum: null,//云剪辑条数
        publicizeShortVideoNum: null,//视频宣传拍摄条数
        date: [], //校验时间用
        frontMoney: '',//前置费
        frontMoneyTime: '',//前置费收取时间
      },
      serviceList: [],
      rules: {
        signTime: [
          { required: true, message: "请选择日期", trigger: ["change"] },
        ],
        date: [
          { required: true, message: "请选择服务时间", trigger: ["change"] },
        ],
        serviceTypeList: [
          { required: true, message: "请选择服务类型", trigger: ["change"] },
        ],
        serviceCommission: [
          {validator: checkServiceCommission,required: true,trigger: ["change"],},
        ],
        liveNum: [
          { validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],
        specialLiveNum: [
          { validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],
        guestLiveNum: [
          { validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],


        shortVideoNum: [
          {validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],
        talentShortVideoNum: [
          {validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],
        cloudShortVideoNum: [
          {validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],
        publicizeShortVideoNum: [
          {validator: checkConsumeCycleRatio,required: true,trigger: ["change"],},
        ],


        responsiblePerson: [
          { required: true, message: "请选择客户负责人", trigger: ["change"] },
        ],
        
        frontMoney: [
          {validator: checkfrontMoney,required: false,trigger: ["change"],},
        ],
      },
    };
  },
  computed: {},
  created() {
    console.log('this.pathType',this.pathType)
    this.getDict();
    if (this.pathType == "edit") {
      this.title = "编辑服务信息";
      this.ruleForm.type = 2
      this.getDetail();
    } else if (this.pathType == "editType") {
      this.ruleForm.type = 1
      this.title = "修改签约状态";
      this.getDetail();
    }
  },
  mounted() {},
  computed: {},
  methods: {
    //获取状态字典
    getDict() {
      var dictType = "bee_client_service_type";
      this.$getSelectList(dictType)
        .then((res) => {
          this.serviceList = res.result[0].dictList;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //获取详情
    getDetail() {
      var id = this.id;
      serviceInfo(id).then((res) => {
        var data = res.result;
        this.ruleForm = {
          ...res.result,
          date: [],
          type: this.pathType == 'edit' ? 2 : 1
        };
        this.ruleForm.date = [data.serviceTimeStart,data.serviceTimeEnd]
      });
    },
    //选择时间
    changeTime(e) {
      this.ruleForm.serviceTimeStart = e[0];
      this.ruleForm.serviceTimeEnd = e[1];
    },

    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    // 计算直播场数
    calculateLiveNum(){
      if(this.ruleForm.specialLiveNum !== '' && this.ruleForm.guestLiveNum !== ''){
        this.ruleForm.liveNum = parseFloat(this.ruleForm.specialLiveNum) + parseFloat(this.ruleForm.guestLiveNum)
      }
    },

    // 计算视频数
    calculateshortVideoNum(){
      if(this.ruleForm.talentShortVideoNum !== '' && this.ruleForm.cloudShortVideoNum !== '' && this.ruleForm.publicizeShortVideoNum !== ''){
        this.ruleForm.shortVideoNum = parseFloat(this.ruleForm.talentShortVideoNum) + parseFloat(this.ruleForm.cloudShortVideoNum) + parseFloat(this.ruleForm.publicizeShortVideoNum)
      }
    },


    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.AgreeLoading = true;
          var data = {
            ...this.ruleForm,
          };
          if (this.pathType == "add") {
            addClient(data)
              .then((res) => {
                this.handleClose();
                this.$message.success("创建成功");
                this.$emit("success");
                this.AgreeLoading = false;
              })
              .finally(() => {
                this.AgreeLoading = false;
              });
          } else if (this.pathType == "edit" || this.pathType == "editType") {
            console.log('data',data);
            editService(data)
              .then((res) => {
                this.handleClose();
                this.$message.success("修改成功");
                this.$emit("success");
                this.AgreeLoading = false;
              })
              .finally(() => {
                this.AgreeLoading = false;
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
  <style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}
.el-select,
.el-input,
.el-cascader {
  width: 400px;
}
.main {
  padding: 20px 20px;
}
.button_box {
  display: flex;
  justify-content: center;
}
.el-radio-group {
  display: flex;
  flex-direction: column;
  .el-radio {
    margin-bottom: 14px;
  }
}
.txt {
  font-size: 20px;
  font-weight: 500;
  color: #333;
  border-bottom: 4px solid #1890ff;
  margin-bottom: 20px;
  padding-bottom: 14px;
}
</style>
  